package com.marvels.system.permission.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.system.permission.dao.SysPositionDao;
import com.marvels.system.permission.dto.SysOrgDTO;
import com.marvels.system.permission.dto.SysPositionDTO;
import com.marvels.system.permission.service.PositionService;

@Service
public class PositionServiceImpl implements PositionService {
	@Autowired
	private SysPositionDao positionDao;

	@Override
	public List<SysPositionDTO> getPositionsByOrgCode(SysOrgDTO org) throws MarvelsBusinessException {
		if(org==null || StringUtils.isEmpty(org.getOrgCode())){
			throw new MarvelsBusinessException("组织机构代码为空");
		}
		List<SysPositionDTO> list = positionDao.selectByOrgCode(org);
		List<SysPositionDTO> listTree = new ArrayList<SysPositionDTO>();
		List<SysPositionDTO> children = null;
		if(CollectionUtils.isNotEmpty(list)){
			for(SysPositionDTO position : list){
				if("-1".equals(position.getParentPositionCode())){
					//解析子节点，拼装成树结构
					children = getChildrenByParent(position,list);
					position.setChildren(children);
					//添加根节点
					listTree.add(position);
				}
			}
		}
		return listTree;
	}
	
	private List<SysPositionDTO> getChildrenByParent(SysPositionDTO parentNode,List<SysPositionDTO> list)throws MarvelsBusinessException{
		List<SysPositionDTO> children = new ArrayList<SysPositionDTO>();
		for(SysPositionDTO child : list){
			if(parentNode.getPositionCode().equals(child.getParentPositionCode())){
				children.add(child);
			}
		}
		if(children.size() > 0){
			for(SysPositionDTO child : children){
				child.setChildren(getChildrenByParent(child,list));
			}
		}
		return children;
	}

	@Override
	public int updatePositionById(SysPositionDTO position) throws MarvelsBusinessException {
		if(position==null || position.getPositionId()==0){
			throw new MarvelsBusinessException("岗位对象id为空");
		}
		position.setUpdatedBy(position.obtainLoginUserName());
		return positionDao.updateByPrimaryKey(position);
	}

	@Override
	public SysPositionDTO addPosition(SysPositionDTO position) throws MarvelsBusinessException {
		if(position==null || StringUtils.isEmpty(position.getPositionCode())){
			throw new MarvelsBusinessException("岗位编码为空");
		}
		if(isPositionCodeExistence(position.getPositionCode())){
			throw new MarvelsBusinessException("岗位编码"+position.getPositionCode()+"已存在");
		}
		position.setCreatedBy(position.obtainLoginUserName());
		position.setUpdatedBy(position.obtainLoginUserName());
		positionDao.insertSelective(position);
		return position;
	}
	
	private boolean isPositionCodeExistence(String positionCode){
		boolean isExisted = false;
		int count = positionDao.selectCountByPositionCode(positionCode);
		if(count > 0){
			isExisted = true;
		}
		return isExisted;
	}
	
	@Override
	public SysPositionDTO addRootPosition(SysPositionDTO position)throws MarvelsBusinessException{
		if(position==null || StringUtils.isEmpty(position.getPositionCode())){
			throw new MarvelsBusinessException("岗位编码为空");
		}
		position.setParentPositionCode("-1");
		position.setParentPositionName(null);
		addPosition(position);
		return position;
	}

	@Override
	public int deletePositionById(SysPositionDTO position) throws MarvelsBusinessException {
		if(position==null || StringUtils.isEmpty(position.getPositionCode())){
			throw new MarvelsBusinessException("岗位编码为空");
		}
		position.setUpdatedBy(position.obtainLoginUserName());
		return positionDao.deleteByPrimaryKey(position);
	}

	@Override
	public SysPositionDTO getPositionById(SysPositionDTO position) throws MarvelsBusinessException {
		if(position==null || position.getPositionId()==0){
			throw new MarvelsBusinessException("岗位对象id为空");
		}
		return positionDao.selectByPrimaryKey(position);
	}

}
